package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

/**
 * 角色模块控制层
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 1.*分页查询角色(根据角色名查询OK...)
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            return roleService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 2.*添加角色
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds, Integer[] menuIds) {
        try {
            roleService.add(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    /**
     * 2.1*添加角色时查询所有权限
     */
    @RequestMapping("/findAllPermission")
    public Result findAllPermission() {
        try {
            List<Permission> permissionList = roleService.findAllPermission();
            return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
    }

    /**
     * 2.2*添加角色时查询所有菜单
     */
    @RequestMapping("/findAllMenu")
    public Result findAllMenu() {
        try {
            List<Menu> menuList = roleService.findAllMenu();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }


    /**
     * 3.1*编辑角色-角色id查询角色
     */
    @RequestMapping("/findById")
    public Result findById(Integer roleId) {
        try {
            Role role = roleService.findById(roleId);
            if (role != null) {
                return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
            } else {
                return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 3.2*编辑角色-根据角色id查询权限ids
     */
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findRoleIdsByRoleId(Integer roleId) {
        try {
            // 返回权限选中ids
            List<Integer> intPermissionList = roleService.findPermissionIdsByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, intPermissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 3.3*编辑角色-根据角色id查询菜单ids
     */
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer roleId) {
        try {
            // 返回菜单选中ids
            List<Integer> intMenuList = roleService.findMenuIdsByRoleId(roleId);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, intMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 3.4*编辑角色-提交角色信息
     */
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[]  permissionIds, Integer[] menuIds) {
        try {
            roleService.edit(role, permissionIds, menuIds);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }



    /**
     * 4.*删除角色(ok...)
     * 1种方案：删除前校验中间表中是否存在数据，如果存在数据，提示“不能删除”
     * 另1种方案：删除中间表，再删除检查组表
     */
    @RequestMapping("/deleteById")
    public Result deleteById(Integer roleId) {
        try {
            roleService.deleteById(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }


}
